﻿using AuthCenter.API.Application.Command.Account;
using AuthCenter.API.DTOs;
using AuthCenter.Common;
using AuthCenter.Domain.Account;
using AuthCenter.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace AuthCenter.API.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class AccountController : ControllerBase
    {
        private readonly IMediator mediator;

        public AccountController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 用户登录接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public Task<APIResult<LoginDTO>> Login(LoginCommand request)
        {

            return mediator.Send(request);
        }
        /// <summary>
        /// 添加用户信息接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> CreateAccount(CreateAccountCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 修改用户信息接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> UpdateAccount(UpdateAccountCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 获取用户信息接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        
        public Task<APIPaging<AccountDTO>> GetAccount([FromQuery]GetAccountCommand request)
        {
            var accountId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            var userName = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
            var roleName = HttpContext.User.FindFirst(ClaimTypes.Role)?.Value;
            var customerValue = HttpContext.User.FindFirst("CustomClaimType")?.Value;
            var Key = HttpContext.User.FindFirst("Key")?.Value;
            return mediator.Send(request);
        }
        /// <summary>
        /// 删除用户信息接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<int>> DelAccount([FromQuery]DelAccountCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 获取省信息接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<ProvincesModel>>> GetProvinces([FromQuery]GetProvincesCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 获取市信息接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<CityModel>>> GetCity([FromQuery] GetCityCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 获取县信息接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<XianModel>>> GetXian([FromQuery] GetXianCommand request)
        {
            return mediator.Send(request);
        }
    }
}
